{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torchvision import models\n",
    "from torchvision import transforms\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# alexnet = models.AlexNet()                 # 实例化AlexNet\n",
    "resnet = models.resnet101(pretrained=True)  # 使用预训练的resnet100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "preprocess = transforms.Compose([           #预处理数据，将输入转化成适合网络输入的形式\n",
    "    transforms.Resize(256),\n",
    "    transforms.CenterCrop(224),\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize(\n",
    "        mean = [0.485,0.456,0.406],\n",
    "        std = [0.229,0.224,0.225]\n",
    "    )\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = Image.open('../data/p1ch2/bobby.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "img_t = preprocess(img)    #预处理\n",
    "batch_t=torch.unsqueeze(img_t,0)    #打平"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ResNet(\n",
       "  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n",
       "  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "  (relu): ReLU(inplace=True)\n",
       "  (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)\n",
       "  (layer1): Sequential(\n",
       "    (0): Bottleneck(\n",
       "      (conv1): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "      (downsample): Sequential(\n",
       "        (0): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      )\n",
       "    )\n",
       "    (1): Bottleneck(\n",
       "      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (2): Bottleneck(\n",
       "      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "  )\n",
       "  (layer2): Sequential(\n",
       "    (0): Bottleneck(\n",
       "      (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "      (downsample): Sequential(\n",
       "        (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)\n",
       "        (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      )\n",
       "    )\n",
       "    (1): Bottleneck(\n",
       "      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (2): Bottleneck(\n",
       "      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (3): Bottleneck(\n",
       "      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "  )\n",
       "  (layer3): Sequential(\n",
       "    (0): Bottleneck(\n",
       "      (conv1): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "      (downsample): Sequential(\n",
       "        (0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(2, 2), bias=False)\n",
       "        (1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      )\n",
       "    )\n",
       "    (1): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (2): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (3): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (4): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (5): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (6): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (7): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (8): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (9): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (10): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (11): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (12): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (13): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (14): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (15): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (16): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (17): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (18): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (19): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (20): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (21): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (22): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "  )\n",
       "  (layer4): Sequential(\n",
       "    (0): Bottleneck(\n",
       "      (conv1): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "      (downsample): Sequential(\n",
       "        (0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(2, 2), bias=False)\n",
       "        (1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      )\n",
       "    )\n",
       "    (1): Bottleneck(\n",
       "      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "    (2): Bottleneck(\n",
       "      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
       "      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "      (relu): ReLU(inplace=True)\n",
       "    )\n",
       "  )\n",
       "  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))\n",
       "  (fc): Linear(in_features=2048, out_features=1000, bias=True)\n",
       ")"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resnet.eval()          #加载预训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "out = resnet(batch_t)          #数据输入网络，得到输出,输出是包含对应1000个类别的分数（1-10）\n",
    "with open('../data/p1ch2/imagenet_classes.txt') as f:\n",
    "    labels = [line.strip() for line in f.readlines()]   #加载1000类物体的labels，这与训练时对应的类别顺序相同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-3.4803e+00, -1.6618e+00, -2.4515e+00, -3.2662e+00, -3.2466e+00,\n",
       "         -1.3611e+00, -2.0465e+00, -2.5112e+00, -1.3043e+00, -2.8900e+00,\n",
       "         -1.6862e+00, -1.3055e+00, -2.6129e+00, -2.9645e+00, -2.4300e+00,\n",
       "         -2.8143e+00, -3.3019e+00, -7.9404e-01, -6.5183e-01, -1.2308e+00,\n",
       "         -3.0193e+00, -3.9457e+00, -2.2675e+00, -1.0811e+00, -1.0232e+00,\n",
       "         -1.0442e+00, -3.0918e+00, -2.4613e+00, -2.1964e+00, -3.2354e+00,\n",
       "         -3.3013e+00, -1.8553e+00, -2.0921e+00, -2.1327e+00, -1.9102e+00,\n",
       "         -3.2403e+00, -1.1396e+00, -1.0925e+00, -1.2186e+00, -9.3332e-01,\n",
       "         -4.5093e-01, -1.5489e+00,  1.4161e+00,  1.0871e-01, -1.8442e+00,\n",
       "         -1.4806e+00,  9.6227e-01, -9.9456e-01, -3.0060e+00, -2.7384e+00,\n",
       "         -2.5798e+00, -2.0666e+00, -1.8022e+00, -1.9328e+00, -1.7726e+00,\n",
       "         -1.3041e+00, -4.5848e-01, -2.0537e+00, -3.2804e+00, -5.0451e-01,\n",
       "         -3.8174e-01, -1.1147e+00, -7.3998e-01, -1.4299e+00, -1.4883e+00,\n",
       "         -2.1073e+00, -1.7373e+00, -4.0412e-01, -1.9374e+00, -1.4862e+00,\n",
       "         -1.2102e+00, -1.3223e+00, -1.0832e+00,  7.9209e-02, -4.1344e-01,\n",
       "         -2.7477e-01, -8.5398e-01,  6.0365e-01, -8.9196e-01,  1.4761e+00,\n",
       "         -2.6427e+00, -3.6478e+00, -2.7066e-01, -1.2360e-01, -2.2445e+00,\n",
       "         -2.3425e+00, -1.4430e+00,  2.5264e-01, -1.0588e+00, -2.8812e+00,\n",
       "         -2.5145e+00, -2.2579e+00,  4.1647e-01, -1.3463e+00, -1.6450e-02,\n",
       "         -2.8798e+00, -5.5658e-01, -1.3859e+00, -2.9352e+00, -1.8880e+00,\n",
       "         -4.2244e+00, -2.9742e+00, -2.0298e+00, -2.3869e+00, -2.7324e+00,\n",
       "         -3.9905e+00, -3.6113e+00, -5.4423e-01, -1.0291e+00, -1.8998e+00,\n",
       "         -3.5611e+00, -1.5031e+00,  1.0660e+00, -7.1587e-01, -7.2612e-01,\n",
       "         -2.2173e+00, -2.2616e+00, -5.9990e-01, -1.4349e+00, -2.5965e+00,\n",
       "         -3.9844e+00, -9.4164e-01, -5.3675e-01, -8.4138e-01, -1.1660e+00,\n",
       "         -7.3556e-01, -1.1300e+00, -2.1074e+00, -4.0037e+00, -3.7230e-01,\n",
       "         -2.7179e+00, -2.9849e+00, -1.9127e+00, -1.8412e+00, -1.3001e+00,\n",
       "         -2.2268e+00, -2.0247e+00, -3.1761e+00, -3.2964e+00, -2.7923e+00,\n",
       "         -4.3191e-01, -3.7750e+00, -2.4832e+00, -2.6228e+00, -2.7499e+00,\n",
       "         -2.6306e+00, -3.2714e+00, -4.3249e+00, -4.2451e+00, -3.6207e+00,\n",
       "         -1.1967e+00,  2.3839e+00,  1.8833e+00,  2.2390e+00,  4.9467e+00,\n",
       "          9.9434e-01,  2.9570e+00,  8.5852e-01,  2.2356e+00,  6.1872e+00,\n",
       "          4.2074e+00,  4.6280e+00,  7.5066e+00,  4.3456e+00,  4.8873e+00,\n",
       "          5.8086e+00,  4.0282e+00,  3.5778e+00,  9.5398e+00,  1.0959e+00,\n",
       "          3.3065e+00,  1.9473e+00, -4.7347e-01,  1.4388e+00,  1.8860e+00,\n",
       "          5.5149e+00,  5.6885e+00,  2.1434e+00,  2.5016e+00,  6.2614e-01,\n",
       "          1.9095e+00,  1.4927e+00,  3.4522e+00,  4.0987e-01,  4.2790e+00,\n",
       "          4.3379e+00,  1.2945e+00,  1.6308e+00,  1.1426e+00,  2.1246e+00,\n",
       "          8.6189e-01,  3.0266e+00,  3.5030e+00,  2.7914e+00,  1.8812e+00,\n",
       "          1.3916e-01,  2.0182e+00,  2.6938e+00,  1.0643e+00,  1.9063e+00,\n",
       "          3.5028e+00,  2.2950e+00,  2.5388e+00,  1.3140e+00,  3.5698e+00,\n",
       "          7.7051e+00,  4.3443e+00,  1.5674e+01,  1.2140e+01,  5.2050e+00,\n",
       "          1.9331e+00,  5.4996e+00,  6.1745e+00,  7.5155e+00,  5.8567e+00,\n",
       "          6.9794e+00,  5.6891e+00,  2.6934e+00,  5.3248e+00,  9.8436e+00,\n",
       "          6.4168e+00,  2.4431e+00,  5.6031e+00,  3.4884e+00,  2.0732e+00,\n",
       "          1.3375e+00,  2.5550e+00,  5.7791e+00,  7.5825e-01,  1.0360e+00,\n",
       "          4.8250e+00,  5.9932e+00,  3.9907e+00, -1.7508e+00,  3.6606e+00,\n",
       "          2.8820e+00,  2.8978e+00,  1.3059e+00,  4.2622e+00,  4.0880e+00,\n",
       "          3.4181e+00,  2.3945e+00,  3.1604e-01,  8.7091e-01,  5.0895e+00,\n",
       "         -7.0908e-01,  1.9885e+00,  2.8699e+00,  2.5281e+00,  1.9253e+00,\n",
       "          6.5843e-01,  3.4956e+00, -5.6701e-01,  1.9219e+00,  5.0423e-01,\n",
       "          2.3949e+00,  3.4628e+00,  5.1851e+00,  1.8182e+00,  3.9127e+00,\n",
       "          4.3620e+00,  3.3723e-01, -4.6588e-01,  5.6958e+00,  3.7192e+00,\n",
       "          2.4205e+00,  3.6402e+00,  3.3705e+00, -9.3733e-01, -2.0590e-01,\n",
       "          1.3018e-01,  1.1554e+00, -4.0951e-02,  4.5523e+00, -1.8349e+00,\n",
       "         -2.6543e+00, -1.6859e+00, -6.3751e-01, -1.5596e+00, -2.1529e+00,\n",
       "         -1.0245e+00,  1.5312e+00,  7.6857e-01, -1.8030e+00,  6.9033e-01,\n",
       "          9.1473e-01, -2.0907e+00, -2.1250e+00, -1.5808e+00, -4.7830e+00,\n",
       "         -1.0396e+00, -9.7836e-01, -2.0528e+00,  1.9793e+00, -6.0107e-01,\n",
       "         -2.4964e+00, -1.4914e+00, -3.2041e+00, -1.9067e+00, -5.9215e-01,\n",
       "         -1.0509e+00,  1.3131e+00, -1.5027e+00, -2.0352e+00,  1.3009e+00,\n",
       "          3.9806e-01, -3.5441e-01,  7.1537e-01, -3.0085e-01, -7.6253e-01,\n",
       "         -5.4504e-01,  1.0533e+00,  1.1973e-01,  7.1266e-02,  1.3234e+00,\n",
       "         -2.0051e+00, -1.7127e+00,  1.1415e+00, -4.3746e-01,  2.9573e-01,\n",
       "         -1.4572e+00, -2.6234e+00, -2.5400e+00, -2.4128e-01, -2.3629e+00,\n",
       "         -1.5560e+00, -2.5256e+00, -8.0395e-01,  1.5960e-01, -2.8029e+00,\n",
       "         -1.8937e+00, -9.4297e-01, -3.8988e-01, -4.6732e-01, -7.8798e-01,\n",
       "         -2.5103e+00, -1.8726e+00, -2.1138e+00, -5.5075e-01,  1.8876e-01,\n",
       "         -2.0678e+00, -1.7942e+00, -2.4776e+00, -3.8874e+00, -4.4214e+00,\n",
       "         -2.1606e+00, -1.9960e+00, -3.7195e+00, -1.8627e+00, -3.3882e+00,\n",
       "         -2.0034e+00, -2.2823e+00, -8.3603e-01, -5.1364e-01, -2.9197e+00,\n",
       "         -1.6728e+00, -2.5686e-01, -2.7734e+00, -1.7911e+00,  1.1283e-01,\n",
       "         -2.1215e+00, -1.5402e+00, -1.2457e+00, -9.6399e-01, -2.4953e+00,\n",
       "         -1.3973e+00, -3.8589e+00, -4.3189e+00, -1.5287e+00, -1.9420e+00,\n",
       "         -3.0008e+00, -2.9597e+00, -4.8460e+00, -2.4737e+00, -1.4287e+00,\n",
       "         -2.9093e+00, -1.2882e+00, -6.0873e-01, -2.8312e+00, -1.8754e+00,\n",
       "         -2.3758e+00, -3.4176e+00, -2.5520e+00, -3.8709e+00, -4.4702e+00,\n",
       "         -3.5587e+00, -9.4389e-01, -2.3503e+00, -2.0270e+00, -1.8470e+00,\n",
       "         -3.2897e+00, -3.4712e+00, -2.8471e+00, -1.9893e+00, -3.7441e+00,\n",
       "         -1.1865e+00, -2.8282e+00,  2.2839e-01, -1.3325e-01, -3.1260e-01,\n",
       "          1.4785e-01,  1.7180e+00,  1.8871e+00, -3.1302e+00, -3.7345e+00,\n",
       "         -2.6754e+00, -6.7742e-01, -8.4727e-01, -1.3179e+00,  4.7847e-01,\n",
       "         -2.2918e+00,  4.7733e+00,  1.5100e+00, -1.5956e+00,  3.3496e+00,\n",
       "          3.0611e+00,  1.5253e+00,  6.8673e-01,  1.2918e+00,  1.6387e+00,\n",
       "          1.0631e-01,  1.3420e+00,  5.2415e-02,  1.0270e+00, -4.6863e-01,\n",
       "         -1.3585e+00,  5.7504e-01,  2.8775e-01,  2.8255e+00,  2.1875e+00,\n",
       "          1.8301e+00,  1.3566e+00,  1.0992e+00,  2.3172e+00,  6.4046e+00,\n",
       "          1.8630e+00,  6.0024e-01, -1.4953e+00, -1.9144e+00, -2.6436e+00,\n",
       "          1.5186e+00, -4.8838e-01, -1.0530e-01,  1.9803e+00, -1.7358e+00,\n",
       "          3.7236e-01,  1.6658e+00,  7.8257e-01,  2.1721e+00, -1.4210e+00,\n",
       "         -2.4550e+00,  4.6637e-01,  3.3418e+00, -2.8537e-01,  1.1941e-01,\n",
       "          1.1450e+00, -1.3834e+00,  1.5737e+00, -2.1716e+00, -4.2427e-01,\n",
       "         -1.4805e+00, -2.1745e+00,  2.7962e+00,  2.4990e+00,  1.9236e-01,\n",
       "          4.7498e-01, -1.9682e+00, -1.6105e+00, -7.3869e-01, -1.1794e+00,\n",
       "         -2.9531e-01, -1.4142e+00,  2.2398e+00, -4.3380e-01, -8.6286e-01,\n",
       "          4.0300e-01, -1.4318e+00, -3.1364e-01,  3.4846e+00,  4.3202e-01,\n",
       "          4.5058e-01, -1.1090e+00,  2.2513e-01, -2.6651e+00, -2.8278e+00,\n",
       "         -6.5790e-01, -3.0889e-01,  8.2096e-01,  1.8005e-01, -4.2284e-01,\n",
       "         -5.8541e-01, -2.7820e-01,  1.6590e+00,  8.7697e-02, -4.6728e-01,\n",
       "          1.1241e+00,  2.2742e+00, -1.0448e+00,  9.4819e-01,  9.9525e-01,\n",
       "         -2.5969e+00, -5.5236e-01,  2.1583e+00, -9.2215e-01,  4.7107e-02,\n",
       "         -3.8016e-01,  1.5210e+00, -1.0433e+00,  1.9041e+00,  1.4741e+00,\n",
       "         -4.3896e+00, -1.6206e-01, -1.5698e-01, -7.3738e-01,  1.8179e+00,\n",
       "          3.3264e+00,  7.3696e-01, -7.6419e-01,  1.5898e+00,  1.9445e+00,\n",
       "          1.2725e+00, -1.5624e+00,  2.2197e+00,  9.9570e-01, -6.3256e-01,\n",
       "         -1.4160e+00,  1.6144e+00,  4.5530e-02,  9.0732e-01,  9.5069e-01,\n",
       "          5.3562e-01,  4.4124e-01,  1.0358e+00,  6.5593e-01,  3.3626e+00,\n",
       "         -1.0299e+00, -2.8939e+00, -7.0227e-01, -8.1103e-01,  7.0547e+00,\n",
       "         -3.3097e+00,  1.3230e+00,  1.6968e+00,  3.7732e+00, -1.1723e+00,\n",
       "          5.7985e-01, -1.8231e+00, -1.3483e+00,  4.1487e-01,  2.6429e+00,\n",
       "          1.4418e+00,  7.9635e-01,  4.8719e+00,  1.5457e+00, -3.5932e+00,\n",
       "         -2.2285e+00, -1.3850e+00, -8.9728e-01,  2.1657e+00,  2.0583e+00,\n",
       "         -8.9567e-01, -1.7835e+00, -1.4516e+00,  1.0497e+00, -7.6032e-01,\n",
       "         -1.4353e+00,  4.7010e-01,  8.7255e-01,  6.7030e-01, -1.1902e+00,\n",
       "         -1.4175e+00, -8.4839e-01,  1.1901e+00, -1.8283e+00,  2.4775e+00,\n",
       "          3.4005e-01, -1.7652e+00, -9.1973e-01,  2.9893e+00,  2.2373e+00,\n",
       "         -8.1442e-01, -1.9843e+00,  9.2510e-01, -2.1452e+00,  1.8890e-02,\n",
       "          2.5441e-01, -1.1333e-01, -6.2533e-01,  8.0225e-01,  4.0010e+00,\n",
       "         -1.1935e+00,  2.6455e+00, -1.7860e+00,  7.5864e-01,  5.1593e-01,\n",
       "          2.4370e-03, -7.6759e-01,  4.8149e-01,  1.3055e+00,  8.0364e-01,\n",
       "         -6.1874e-01,  4.6971e-02,  2.6322e-01, -2.1400e+00, -1.3908e+00,\n",
       "         -4.0181e-02, -4.2920e-01,  4.6767e-01,  1.3024e+00,  7.5817e-01,\n",
       "          9.9858e-02, -1.0072e-01, -8.5241e-01,  8.6250e-01,  6.9518e-01,\n",
       "          2.1217e+00,  7.1266e-01, -1.9782e-01,  2.3986e+00,  1.8734e+00,\n",
       "          1.0993e+00,  1.0336e+00,  1.4353e+00, -4.9214e-02, -1.3295e-01,\n",
       "         -1.7147e+00, -1.2590e+00, -1.3166e+00, -3.4476e+00,  5.9193e-01,\n",
       "          1.0995e+00,  1.0987e-02, -3.7005e-01, -4.5369e-01, -4.2330e-01,\n",
       "         -1.5137e+00,  2.7933e-01, -2.0776e-01,  3.2132e+00,  1.8063e+00,\n",
       "         -1.5186e+00,  2.8835e+00, -7.4290e-01,  3.2128e-02, -7.0117e-02,\n",
       "         -1.0103e+00,  1.1795e+00,  5.9284e-01,  1.2191e-01, -3.4571e+00,\n",
       "          1.3048e+00,  3.9847e-01, -1.2731e+00, -1.2927e+00, -1.6408e+00,\n",
       "          1.9229e+00,  4.1587e-02, -9.8906e-01,  6.7141e-01,  2.8807e+00,\n",
       "          1.6977e+00,  2.2304e-01, -8.1440e-01, -2.0507e+00,  1.7015e+00,\n",
       "         -2.0312e-01,  7.4630e-01,  1.5227e+00, -1.4377e+00, -1.1784e+00,\n",
       "          5.1375e-01, -6.4234e-01,  3.8709e-02,  2.6664e+00, -1.6256e+00,\n",
       "         -3.3457e+00,  2.1520e+00,  8.6618e-01,  1.3850e+00, -3.4029e-01,\n",
       "          1.8385e-01,  1.4680e+00, -1.0961e+00,  1.8217e+00, -1.2748e+00,\n",
       "         -2.1175e+00, -8.4856e-01, -5.3657e-01, -1.2562e+00,  1.1329e+00,\n",
       "         -1.4191e+00, -7.6893e-01, -3.4133e-01,  2.1594e+00, -2.1836e-01,\n",
       "         -1.8166e+00,  9.8038e-02,  1.7366e+00,  1.6465e-01,  7.7770e-01,\n",
       "          4.7226e+00, -7.3754e-01, -1.6683e+00, -8.1360e-01, -1.4618e+00,\n",
       "          3.4068e+00,  5.3348e-01, -3.1106e-01, -5.0764e-01,  3.0037e-01,\n",
       "          1.8626e+00, -1.1852e+00, -2.0411e+00, -9.6967e-02, -7.1424e-01,\n",
       "         -2.5433e+00, -3.4143e-02,  7.6702e-01, -1.7948e+00,  2.9510e-01,\n",
       "         -1.0903e+00,  1.5320e+00,  2.8823e+00,  5.1182e-01, -7.6857e-01,\n",
       "         -9.0145e-01, -1.7196e+00, -1.0044e+00,  9.1568e-01, -9.2977e-02,\n",
       "         -2.3068e+00,  2.2911e+00,  9.5719e-01,  1.9917e+00, -1.6980e+00,\n",
       "          2.6118e+00,  3.7953e+00,  7.1091e-01, -2.2806e-03, -1.0275e+00,\n",
       "          2.1824e+00,  1.4127e+00,  4.7933e-01, -1.3249e+00, -9.0533e-01,\n",
       "          5.8118e-01, -6.0400e-01,  5.1156e-01,  1.1511e+00,  9.5682e-01,\n",
       "          2.7826e+00, -3.0976e+00,  3.5563e+00, -1.6181e-01, -4.6197e-02,\n",
       "         -2.0769e+00, -1.4204e+00,  2.9824e+00, -4.8723e-01,  2.1408e-01,\n",
       "         -1.3643e-01,  2.2942e+00,  3.4084e-01,  9.9796e-01, -1.1452e+00,\n",
       "          3.3055e+00, -1.8049e+00,  3.2445e+00, -1.6493e-01,  1.3805e+00,\n",
       "          6.5878e-01,  4.6122e-01, -7.8641e-01,  3.8983e-01,  1.9974e+00,\n",
       "          4.0911e-01,  2.4161e+00, -1.9111e+00,  8.1045e-02,  2.2694e+00,\n",
       "         -1.6680e+00, -7.0304e-01,  1.4299e+00,  1.4232e-02,  7.9249e-01,\n",
       "          2.9637e+00, -9.4825e-01, -1.3366e+00,  2.6750e-01,  2.3589e+00,\n",
       "          1.8983e+00,  1.8345e+00,  8.5127e-01,  4.2841e+00,  4.8082e-01,\n",
       "         -1.4365e+00, -4.8286e-01,  3.0412e+00, -8.2025e-01,  3.3065e+00,\n",
       "         -6.5939e-01, -2.6282e+00, -3.1888e+00, -2.9725e+00,  1.2156e+00,\n",
       "          5.6016e+00,  3.0274e-01, -3.1681e+00,  2.5582e+00, -3.3199e-01,\n",
       "          1.4820e-01,  2.3601e+00, -1.4552e+00,  3.3269e+00, -3.3744e+00,\n",
       "         -6.4104e-01,  1.1680e+00, -2.6107e+00,  1.6885e+00, -1.5028e+00,\n",
       "         -2.6845e+00, -3.6659e+00, -1.7394e+00,  1.1231e+00,  2.0104e+00,\n",
       "         -1.4943e-01,  1.3057e+00,  1.2092e+00,  2.6647e+00, -1.7969e+00,\n",
       "         -1.8525e+00,  1.5487e+00, -2.0861e+00, -2.3154e+00,  9.9215e-01,\n",
       "         -3.7871e+00, -1.1176e+00,  9.0636e-01, -3.2947e-01, -3.4544e+00,\n",
       "          2.0940e+00,  5.4372e-01,  6.0876e-01, -1.3066e-01,  7.9443e-01,\n",
       "          7.9938e-01,  1.0587e+00, -1.8372e+00,  2.8466e-01, -1.1158e+00,\n",
       "          8.0786e-01,  1.0870e+00,  8.9547e+00, -8.9419e-01, -9.3960e-01,\n",
       "          1.0807e+00, -4.1462e-01, -1.7524e+00,  9.1856e-02,  1.8185e-01,\n",
       "         -1.3849e+00,  8.8831e-01, -4.1253e-01, -7.7844e-01, -3.1265e+00,\n",
       "         -3.8734e-01,  1.8115e-01, -2.2122e+00,  2.8848e+00,  4.5000e-01,\n",
       "          1.4854e+00, -3.4138e+00,  1.4939e+00, -2.5266e+00, -2.9228e+00,\n",
       "         -7.6507e-01,  2.8269e+00, -1.1918e+00, -6.2602e-01,  3.6187e+00,\n",
       "          1.1527e+00,  1.1860e+00,  3.4149e+00,  9.2982e-01, -1.1376e+00,\n",
       "          1.0391e+00,  1.8575e-01, -7.4427e-01, -2.9312e+00, -1.6815e-01,\n",
       "          1.5624e+00, -4.5063e-01,  1.5997e+00,  1.0128e+00, -1.3146e+00,\n",
       "         -1.8426e+00, -4.7445e-01,  5.8991e-01,  2.3850e+00,  5.2548e-01,\n",
       "         -1.3760e+00, -2.3240e+00, -7.6861e-01,  1.2772e+00,  2.9579e+00,\n",
       "         -2.7968e-01, -5.9378e-01, -2.4310e-02, -7.2352e-01, -5.9499e-02,\n",
       "          2.7550e+00,  2.9499e-01, -1.1396e+00, -1.4785e+00, -4.3375e+00,\n",
       "         -3.2104e-01, -3.2125e-01, -2.0806e+00,  3.7004e-01, -1.4368e+00,\n",
       "         -6.1700e-01, -2.0341e+00, -8.6155e-01, -4.0387e-01, -3.2359e-01,\n",
       "         -1.8287e+00, -1.7554e+00, -6.5640e-01,  6.7694e-01,  3.7156e+00,\n",
       "          2.1207e+00,  4.0970e+00,  1.7257e+00,  8.5265e-01,  1.2722e+00,\n",
       "          1.0563e+00,  1.3809e+00,  1.2871e+00, -7.5314e-01,  2.2593e+00,\n",
       "          1.1952e-01, -7.3866e-01,  1.0060e+00,  8.5880e-01, -6.6744e-01,\n",
       "         -3.2016e-01, -1.5605e+00,  2.0461e+00,  2.4740e+00,  2.2464e-01,\n",
       "          7.4987e-01,  3.8843e-02, -1.7622e+00,  1.9534e+00,  4.5175e-01,\n",
       "          1.2086e+00,  7.3219e-01, -1.0001e+00,  1.2820e-01, -3.7380e-01,\n",
       "          9.6213e-02,  3.2060e+00,  6.5023e-01, -1.1252e-01,  8.9641e-01,\n",
       "         -5.2855e-02, -1.1584e+00,  1.4922e-01,  3.7309e-01,  8.7084e-01,\n",
       "         -1.9354e+00,  1.0733e-01, -1.5175e+00, -1.8582e+00, -3.8437e+00,\n",
       "          1.8629e-01, -2.9438e+00,  5.4171e-01, -7.8057e-01, -2.6016e+00,\n",
       "         -4.4594e+00,  5.5604e-01, -1.3140e+00, -3.8407e+00, -7.5988e-01,\n",
       "         -5.7457e-01, -2.5448e+00,  2.3831e+00,  6.1368e-01,  4.8296e-01,\n",
       "          2.8674e+00, -3.7442e+00,  1.5085e+00, -3.2500e+00, -2.4894e+00,\n",
       "         -3.3541e-01,  1.2856e-01, -1.1355e+00,  3.3969e+00,  4.4584e+00]],\n",
       "       grad_fn=<AddmmBackward>)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "out\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('golden retriever', 96.29334259033203)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_,index = torch.max(out,1)          # 返回1维度上的最大值及其索引，index是一个1维张量 index[0]才代表具体的索引数\n",
    "percentage = torch.nn.functional.softmax(out,dim=1)[0]*100  # 将所有的输出标准化为（0-1）然后乘以100来得到百分数  \n",
    "\n",
    "\n",
    "labels[index[0]],percentage[index[0]].item()   # 分别在类别中和分数中索引到最大的类别和分数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('golden retriever', 96.29334259033203),\n",
       " ('Labrador retriever', 2.80812406539917),\n",
       " ('cocker spaniel, English cocker spaniel, cocker', 0.28267455101013184),\n",
       " ('redbone', 0.20863120257854462),\n",
       " ('tennis ball', 0.1162160336971283)]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_,indices = torch.sort(out,descending=True)  # 对所有结果排序\n",
    "[(labels[idx],percentage[idx].item()) for idx in indices[0][:5]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('golden retriever', 96.29334259033203)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
